﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Demo.Services.Contracts.DTO.Infos;
using Demo.Services.Integration.DAO.Impl;
using Demo.CommonBase.Logging;
using Demo.CommonBase.Exceptions;
using System.Reflection;
using Demo.CommonBase.DTO.Infos;
using Demo.CommonBase.DTO.Filters;
using Demo.Services.Contracts.DTO.Filters;
using Demo.Services.Services.Impl.DomainObjects.Helppers;

namespace Demo.Services.Services.Impl.DomainObjects
{
    public class ResourceDomainObject
    {
        public ResourceResultInfo GetResources(PaginationInfo pagin, ResourceFilter filters)
        {
            ResourceResultInfo result = null;
            ResourceDAO resourceDao;
            try
            {
                //Logger.Info();
                resourceDao = new ResourceDAO();
                result = resourceDao.GetResources(pagin, filters);
            }

            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }
            return result;
        }
        public ResourceInfo GetResourceById(int id)
        {
            ResourceInfo result = null;
            ResourceDAO resourceDao;            
            try
            {
                Logger.Info();
                resourceDao = new ResourceDAO();
                result = resourceDao.GetResourceById(id);
                if (result != null)
                {
                    result.TecnologyList = getAssignedTecnologies(id);
                }
            }

            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }
            return result;
        }
        public void DeleteResource(int id)
        {            
            ResourceDAO resourceDao;
            try
            {
                //Logger.Info();
                resourceDao = new ResourceDAO();
                resourceDao.DeleteResource(id);
            }

            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }            
        }
        public void SaveResource(ResourceInfo resource)
        {            
            ResourceDAO resourceDao;
            try
            {
                //Logger.Info();
                resourceDao = new ResourceDAO();
                if (resource.Id != 0)
                {
                    resourceDao.UpdateResource(resource);
                }
                else 
                {
                    int id = resourceDao.SaveResource(resource);
                    resource.Id = id;
                }
                if (resource.TecnologyList == null) {
                    resource.TecnologyList = new List<TecnologyInfo>();
                }
                string tecnoogyList = XMLHelper.GetXML(resource.TecnologyList);
                resourceDao.SaveResourceDetail(resource.Id, tecnoogyList);
            }

            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }            
        }

        public CommonComboBoxPagingInfo GetResourceByName(ComboBoxFilter filter)
        {
            CommonComboBoxPagingInfo result = null;
            ResourceDAO resourcedao;
            try
            {
                Logger.Info();
                resourcedao = new ResourceDAO();
                result = resourcedao.GetRersource_ByName(filter);
            }

            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }
            return result;
        }

        public List<TecnologyInfo> getAssignedTecnologies(int resourceId)
        {
            List<TecnologyInfo> result = null;
            ResourceDAO resourceDao;
            try
            {
                //Logger.Info();
                resourceDao = new ResourceDAO();
                result = resourceDao.GetAssignedTecnologies(resourceId);
            }

            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }
            return result;
        }

        public List<TecnologyInfo> getUnAssignedTecnologies(int resourceId)
        {
            List<TecnologyInfo> result = null;
            ResourceDAO resourceDao;
            try
            {
                //Logger.Info();
                resourceDao = new ResourceDAO();
                result = resourceDao.GetUnAssignedTecnologies(resourceId);
            }

            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }
            return result;
        }

        public IList<ProjectReportInfo> GetResourceAssignment()
        {
            IList<ProjectReportInfo> result = null;
            ResourceDAO resourceDao;
            try
            {
                //Logger.Info();
                resourceDao = new ResourceDAO();
                result = resourceDao.GetResourceAssignment();
            }

            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }
            return result;
        }

        public bool ValidationResource(ResourceFilter filter)
        {
            bool result = false;
            ResourceDAO resourceDao;
            try
            {
                Logger.Info();
                resourceDao = new ResourceDAO();
                result = resourceDao.ValidationResource(filter);
            }

            catch (Exception ex)
            {
                Logger.Error(ex);
                throw new UnhandledException(MethodInfo.GetCurrentMethod(), ex);
            }
            return result;
        }
        
    }
}
